﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.IO;
using System.Linq;
using System.Text;

namespace Swd05.SH.Adam
{
	class Program
	{
		#region InnerClasses
		#region ScriptParser
		public class ScriptParser
		{
			private StringCollection coll = new StringCollection();
			private StringReader sr = null;
			public ScriptParser(string script)
			{
				this.sr = new StringReader(script);
			}
			#region Properties
			private StringCollection StringCollection
			{
				get
				{
					return this.coll;
				}
			}
			private StringReader Reader
			{
				get
				{
					return this.sr;
				}
			}
			internal StringCollection Scripts
			{
				get
				{
					return this.coll;
				}
			}
			#endregion
			#region Methods
			internal void Parse()
			{
				string line = "start";
				StringBuilder sb = null;
				try
				{
					/*
						anweisung
						GO
						anweisung
						GO
					*/

					sb = new StringBuilder();

					while ((line = this.Reader.ReadLine()) != null)
					{
						if (line.Trim() == "GO")
						{
							// ScriptEnde erreicht
							this.StringCollection.Add(sb.ToString());
							sb.Length = 0;
						}
						else
						{
							sb.Append(line);
							sb.Append(System.Environment.NewLine);
						}
					}
				}
				catch (Exception ex)
				{
					throw ex;
				}
				finally
				{
				}
			}
			#endregion
		}
		#endregion
		#endregion
		#region methods
		static void Main(string[] args)
		{
			Program p = new Program();
			p.Start();
		}
		void Start()
		{
			string key = string.Empty;
			try
			{
				do
				{
					Console.WriteLine("************************************************************");
					Console.WriteLine("*  Bitte wählen ...                                        *");
					Console.WriteLine("*  1 = Datenbank erstellen                                 *");
					Console.WriteLine("* -------------------------------------------------------  *");
					Console.WriteLine("* q = Ende                                                 *");
					Console.WriteLine("************************************************************");

					key = Console.ReadLine().ToLower();

					if (key.Length != 0)
					{
						key = key.Substring(0, 1);
					}

					switch (key)
					{
						case "1":
							this.CreateDatabase();
							break;
						default:
							break;
					}
				} while (key != "q");
			}
			catch (Exception ex)
			{
				Console.WriteLine("Ein Fehler ist aufgetreten");
				Console.WriteLine("--------------------------");
				Console.WriteLine(ex.ToString());
				Console.ReadLine();
			}
		}
		private void CreateDatabase()
		{
			this.RunDbScript( this.LoadScriptById( "Adam.Db.sql.db.drop.sql" ) );
			this.RunDbScripts();
		}
		private void RunDbScripts()
		{
			string script = string.Empty;
			int counter = 0;

			while (true)
			{
				script = this.LoadScriptById(string.Format("Adam.Db.sql.script.{0}.sql", counter.ToString()));
				counter++;
				if (script.Length == 0)
				{
					break;
				}
				this.RunDbScript(script);
			}
			return;
		}
		private string LoadScriptById(string id)
		{
			String script = String.Empty;
			using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(id))
			{
				if (stream != null)
				{
					using (StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default))
					{
						script = sr.ReadToEnd();
					}
				}
			}
			return script;
		}
		private void RunDbScript(string script)
		{
			System.Data.SqlClient.SqlCommand cmd = null;
			ScriptParser p = null;

			p = new ScriptParser(script);
			p.Parse();

			using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(this.Connectionstring))
			{
				conn.Open();
				foreach (string s in p.Scripts)
				{

					cmd = new System.Data.SqlClient.SqlCommand(s.Replace("{database}", this.Database), conn);
					cmd.ExecuteNonQuery();
				}
			}
		}
		#endregion
		#region Properties
		private string Connectionstring
		{
			get
			{
				return System.Configuration.ConfigurationSettings.AppSettings["Connectionstring"].ToString();
			}
		}
		private string Database
		{
			get
			{
				return System.Configuration.ConfigurationSettings.AppSettings["Database"].ToString();
			}
		}
		#endregion
	}
}
